﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace GestorDeFlotasDesktop.AbmCliente
{
    public partial class Listado_Clientes : Form
    {
        private Modelo.FormActions.Actions action;
        public Listado_Clientes(Modelo.FormActions.Actions runAction)
        {
            action = runAction;
            InitializeComponent();
            if (action == Modelo.FormActions.Actions.Baja || action == Modelo.FormActions.Actions.Modificacion)
            {
                using (SqlConnection con = Modelo.Modelo.createConnection())
                {
                    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM NUNCA_TAXI.Clientes WHERE Id_Cliente != 1", con);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    dataGridView1.DataSource = dt;
                    dt.Columns.Add("Modificar");
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dt.Rows[i]["Modificar"] = "Modificar";
                    }

                    try
                    {
                        Modelo.Modelo.openConnection(con);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        Modelo.Modelo.closeConnection(con);
                    }
                }
            }
        }

        private void Listado_Clientes_Load(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 9)
            {
                DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[0];
                Login.Login.mainForm.openForm(new AbmCliente(action, cell.Value.ToString()));
            }
        }

        private void limpiar_Click(object sender, EventArgs e)
        {
            tb_apellido.Text = tb_dni.Text = tb_nombre.Text = "";
            using (SqlConnection con = Modelo.Modelo.createConnection())
            {
                SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM NUNCA_TAXI.Clientes WHERE Id_Cliente != 1", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                dt.Columns.Add("Modificar");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["Modificar"] = "Modificar";
                }

                try
                {
                    Modelo.Modelo.openConnection(con);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    Modelo.Modelo.closeConnection(con);
                }
            }
        }

        private void aceptar_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = Modelo.Modelo.createConnection())
            {
                string consulta = "";
                if (tb_apellido.Text == "" && tb_nombre.Text == "" && tb_dni.Text == "")
                {
                    consulta = "SELECT * FROM NUNCA_TAXI.Clientes WHERE Id_Cliente != 1";
                }
                else if (tb_dni.Text == "")
                {
                    consulta = "SELECT * FROM NUNCA_TAXI.Clientes WHERE (Id_Cliente != 1) AND (Cl_Nombre LIKE '%" + tb_nombre.Text + "%' AND Cl_Apellido LIKE '%" + tb_apellido.Text + "%')";
                }
                else
                {
                    consulta = "SELECT * FROM NUNCA_TAXI.Clientes WHERE (Id_Cliente != 1) AND (Cl_Nombre LIKE '%" + tb_nombre.Text + "%' AND Cl_Apellido LIKE '%" + tb_apellido.Text + "%' AND Cl_Dni = '" + tb_dni.Text + "')";
                }
                SqlDataAdapter da = new SqlDataAdapter(consulta, con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dt.Columns.Add("Modificar");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["Modificar"] = "Modificar";
                }
                dataGridView1.DataSource = dt;
            }
        }
    }
}
